package com.linjian.togetherbirthday.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.linjian.togetherbirthday.R;
import com.linjian.togetherbirthday.activity.SplashActivity;
import com.linjian.togetherbirthday.base.BaseFragment;
import com.linjian.togetherbirthday.contant.HttpContant;
import com.linjian.togetherbirthday.utils.HttpUtils;
import com.linjian.togetherbirthday.utils.NoticeEvent;
import com.linjian.togetherbirthday.utils.SharedPreferencesUtil;
import com.sdsmdg.tastytoast.TastyToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RegistFragment extends BaseFragment implements  View.OnClickListener {

    private CardView cvAdd;
    private View mView;
    private Button btGo;
    private Bitmap bitmap;
    private EditText etSet,etRepeat;

    SplashActivity activity;

    public static RegistFragment newInstance(String phone) {
        RegistFragment registFragment = new RegistFragment();
        Bundle bundle = new Bundle();
        bundle.putString("phone",phone);
        registFragment.setArguments(bundle);
        return registFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int setView() {
        return R.layout.fragmnet_regist;
    }

    @Override
    protected void init(View view) {
        mView = view;
        activity = (SplashActivity) getActivity();
        cvAdd = view.findViewById(R.id.cv_add);
        btGo = view.findViewById(R.id.bt_go_regist);
        etSet = view.findViewById(R.id.et_set_password);
        etRepeat = view.findViewById(R.id.et_repeat_password);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        setListener();
    }
    private void setListener() {
        btGo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_go_regist:
                if (TextUtils.isEmpty(etSet.getText())||TextUtils.isEmpty(etRepeat.getText())){
                    TastyToast.makeText(getActivity(),"密码不能为空",TastyToast.LENGTH_SHORT,TastyToast.WARNING).show();
                }else if (!etSet.getText().toString().equals(etRepeat.getText().toString())){
                    TastyToast.makeText(getActivity(),"重复密码不一致",TastyToast.LENGTH_SHORT,TastyToast.WARNING).show();
                }else {
                    htppSetPassword(etSet.getText().toString(),etRepeat.getText().toString());
                }
                break;
        }
    }

    /**
     * 请求设置密码
     */
    private void htppSetPassword(String password,String pwd){
        Log.i("LoginFragment","SESSION="+String.valueOf(SharedPreferencesUtil.get(getActivity(),"Cookie","")));
        Map<String,Object> map = new HashMap<>();
        map.put("password",password);
        map.put("pwd",pwd);
        HttpUtils.Put(HttpContant.URL + HttpContant.SetPassword
                ,"SESSION="+String.valueOf(SharedPreferencesUtil.get(getActivity(),"Cookie",""))
                ,"type"
                ,"1"
                , map
                , new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        EventBus.getDefault().post(new NoticeEvent("requestFail"));
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        EventBus.getDefault().post(new NoticeEvent("setPassword",response.body().string()));
                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMsg(NoticeEvent event){
        switch (event.getCls()){
            case "requestFail":
                TastyToast.makeText(getActivity(),"请求失败",TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();
                break;
            case "setPassword":
                try {
                    JSONObject object = new JSONObject(event.getData().toString());
                    if (object.getInt("code") == 200){
                        SharedPreferencesUtil.put(getActivity(),"LoginPhone",getArguments().getString("phone"));
                        activity.replaceFragment("ChooseGenderFragment","1");
                    }else {
                        TastyToast.makeText(getActivity(),object.getString("msg"),TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
